package com.hms.rs.controller;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
import org.glassfish.jersey.process.Inflector;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.server.model.ResourceMethod;

public class MyApp extends ResourceConfig{

	// Create JAX-RS application.
	//final Application application = new ResourceConfig().packages("com.hms.app.ui.beans").registerInstances(new JsonMoxyConfigurationContextResolver());
		        
	/* @Override
	    public Set<Class<?>> getClasses() {
	        return new HashSet<Class<?>>() {{
	            // Add your resources.
	        	System.out.println("From the Myapp...");
	            add(PatientController.class);
	            //add(MOXyJsonProvider.class);

	            // Add LoggingFilter.
	            add(LoggingFilter.class);
	        }};
	    }*/
	 
	 /*@Override
	    public Set<Object> getSingletons() {
		 
		 	System.out.println("Configuring MOXy JSON Provider as singleton....");
	        MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();

	        //moxyJsonProvider.setAttributePrefix("@");
	        moxyJsonProvider.setFormattedOutput(true);
	        moxyJsonProvider.setIncludeRoot(true);
	        moxyJsonProvider.setMarshalEmptyCollections(false);
	        
	        //moxyJsonProvider.setValueWrapper("$");
	 
	        Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
	        namespacePrefixMapper.put("http://www.example.org/customer", "cust");
	        moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
	        moxyJsonProvider.setNamespaceSeparator(':');
	 
	        HashSet<Object> set = new HashSet<Object>(1);
	        set.add(moxyJsonProvider);
	        return set;
	    }*/
	 
	 	@Provider
	    final static class JsonMoxyConfigurationContextResolver implements ContextResolver<MoxyJsonConfig> {

	        @Override
	        public MoxyJsonConfig getContext(Class<?> objectType) {
	        	System.out.println("In the overriden moxy config...");
	            final MoxyJsonConfig configuration = new MoxyJsonConfig();

	            Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
	            namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");

	            configuration.setNamespacePrefixMapper(namespacePrefixMapper);
	            configuration.setNamespaceSeparator(':');

	            return configuration;
	        }
	    }
	 	
	 	public MyApp() {
	 
	 		/*final Resource.Builder resourceBuilder = Resource.builder();
	        resourceBuilder.path("helloworld");
	 
	        final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET");
	        methodBuilder.produces(MediaType.TEXT_PLAIN_TYPE)
	                .handledBy(new Inflector<ContainerRequestContext, String>() {
	 
	            @Override
	            public String apply(ContainerRequestContext containerRequestContext) {
	                return "Hello World!";
	            }
	        });
	 
	        final Resource resource = resourceBuilder.build();
	        registerResources(resource);
	        */
	        
	 		System.out.println("In the MyApp.java constructor...");
	 		
	 		registerInstances(new JsonMoxyConfigurationContextResolver());
	 		
	    }
}
